package bianyiyuanli;

import java.io.*;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Scanner;

/**
 * @author Godc
 * @description
 */
public class LexicalAnalyzer {
    static HashMap<String,Integer> map;
    static {
        map = new HashMap<>();
        initMap();
    }

    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        File file = new File("C:\\Users\\叫我彭于晏\\Desktop\\lexical-analyzer\\src\\main\\java\\bianyiyuanli\\content.txt");
        String str = null;
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                String s1 = prepareDeal(s);
                analyzer(s1);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }

    }
    public static void analyzer(String str){
        int i = 0,j=0;
        StringBuffer buffer = new StringBuffer();
        while(i<str.length()){
            // 标识符或者保留字
            if(Character.isLetter(str.charAt(i))){
                buffer.append(str.charAt(i));
                i++;
                // 直到下一个字符字母或者数字结束
                while(i<str.length()&&(isDigit(str.charAt(i))||Character.isLetter(str.charAt(i))||str.charAt(i)=='.')){
                    buffer.append(str.charAt(i));
                    i++;
                }
                if(map.containsKey(buffer.toString())){
                    System.out.println(map.get(buffer.toString())+","+buffer.toString());
                }else {
                    System.out.println(map.get("ID")+","+buffer.toString());
                }
                buffer = new StringBuffer();
                continue;
            }
            else if(i<str.length()&&isDigit(str.charAt(i))){
                int flag = 0;
                buffer.append(str.charAt(i));
                i++;
                while(i<str.length()&&(isDigit(str.charAt(i))||str.charAt(i)=='.')){
                    if(str.charAt(i)=='.') flag = 1;
                    buffer.append(str.charAt(i));
                    i++;
                }
                if(flag == 0)
                    System.out.println(map.get("int")+","+buffer.toString());
                else System.out.println(map.get("float")+","+buffer.toString());
                buffer = new StringBuffer();
                continue;
            }
            else if(str.charAt(i)==' '){
                i++;
                continue;
            }
            else System.out.println(map.get(str.charAt(i)+"")+","+str.charAt(i));
            i++;
        }
    }

    public static String prepareDeal(String str){
            if(str.length()==0) return "";
            int i  = 0;
            str = str.trim();
            // 去掉注释
            if(str.charAt(i)=='/'&&i+1<str.length()&&str.charAt(i+1)=='/'){
                return "";
            }
            // 利用正则表达式用一个空格替换多个空格
            String s1 = str.substring(i, str.length()).replaceAll("\\s+", " ").trim();
            return s1;
    }
    public static boolean isDigit(char c){
        return c>='0'&&c<='9';
    }
    private static void initMap() {
        map.put("main",1);
        map.put("if",2);
        map.put("then",3);
        map.put("while",4);
        map.put("do",5);
        map.put("static",6);
        map.put(";",7);
        map.put("ID",25);
        map.put("NUM",26);
        map.put("+",27);
        map.put("-",28);
        map.put("*",29);
        map.put(":",30);
        map.put(":=",31);
        map.put("/",32);
        map.put("<",33);
        map.put("<>",34);
        map.put("<=",35);
        map.put(">",36);
        map.put(">=",37);
        map.put("=",38);
        map.put("default",39);
        map.put("(",40);
        map.put(")",41);
        map.put("int",42);
        map.put("double",43);
        map.put("struct",44);
        map.put("break",45);
        map.put("else",46);
        map.put("long",47);
        map.put("switch",48);
        map.put("case",49);
        map.put("typedef",50);
        map.put("char",51);
        map.put("return",52);
        map.put("const",53);
        map.put("float",54);
        map.put("short",55);
        map.put("continue",56);
        map.put("for",57);
        map.put("void",58);
        map.put("sizeof",59);
        map.put("#",60);
        map.put("{",61);
        map.put("}",62);
    }
}
